//
//  SelectTimeView.m
//  hello
//
//  Created by 查传申 on 2017/2/15.
//  Copyright © 2017年 查传申. All rights reserved.
//

#import "SelectTimeView.h"

@implementation SelectTimeView
-(id)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        
        self.arr_year = [NSMutableArray array];
        self.arr_month = [NSMutableArray array];
        self.arr_day = [NSMutableArray array];
        self.arr_hour = [NSMutableArray array];
        self.arr_minute = [NSMutableArray array];
        [self load_text_time];
        self.view_button = [[UIView alloc]initWithFrame:CGRectZero];
        _view_button.backgroundColor = [UIColor colorWithRed:240/255.0 green:240/255.0 blue:240/255.0 alpha:1];
        
        [self addSubview:_view_button];
        
        self.button_cancle = [UIButton buttonWithType:UIButtonTypeCustom];
        [_button_cancle addTarget:self action:@selector(buttonCancle) forControlEvents:UIControlEventTouchUpInside];
        [_button_cancle setTitleColor:[UIColor colorWithRed:66/255.0f green:66/255.0f blue:66/255.0f alpha:1] forState:UIControlStateNormal];
        [_button_cancle setTitle:@"取消" forState:UIControlStateNormal];
        _button_cancle.titleLabel.font = [UIFont systemFontOfSize:15];
        [self addSubview:_button_cancle];
        
        self.button_sure = [UIButton buttonWithType:UIButtonTypeCustom];
        [_button_sure addTarget:self action:@selector(buttonSure) forControlEvents:UIControlEventTouchUpInside];
        [_button_sure setTitleColor:[UIColor colorWithRed:66/255.0f green:66/255.0f blue:66/255.0f alpha:1] forState:UIControlStateNormal];
        [_button_sure setTitle:@"确定" forState:UIControlStateNormal];
        _button_sure.titleLabel.font = [UIFont systemFontOfSize:15];
        [self addSubview:_button_sure];

        
        self.pickerView = [[UIPickerView alloc]initWithFrame:CGRectZero];
        
        _pickerView.delegate  =self;
        _pickerView.dataSource = self;
        [self addSubview:_pickerView];
        
        
    }
    return self;
}
-(void)layoutSubviews{
    _view_button.frame = CGRectMake(0, 0, self.frame.size.width, 30);
    
    _button_cancle.frame = CGRectMake(10, 0, 60, 30);
    _button_sure.frame = CGRectMake(self.frame.size.width-70, 0, 60, 30);
    _pickerView.frame = CGRectMake(0, 30, self.frame.size.width, self.frame.size.height-30);
}
-(void)load_text_time{
    for (int i = 1; i<60; i++) {
        if (i<32) {
            if (i>16) {
                
                [_arr_year addObject:[NSString stringWithFormat:@"20%02d年",i]];
            }
            
            if (i<13) {
                [_arr_month addObject:[NSString stringWithFormat:@"%d月",i]];
            }
            [_arr_day addObject:[NSString stringWithFormat:@"%d日",i]];
            
        }
        if (i<24) {
            [_arr_hour addObject:[NSString stringWithFormat:@"%d时",i]];
 
        }
        [_arr_minute addObject:[NSString stringWithFormat:@"%02d分",i]];

        
    }
    [_arr_minute insertObject:@"00分" atIndex:0];

    [_arr_hour insertObject:@"0时" atIndex:0];
    
    
}
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 5;
    
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    if (component==0) {
        return _arr_year.count;
    }else if (component==1){
        return _arr_month.count;
    }else if (component==2){
        return _arr_day.count;
    }else if (component==3){
        return _arr_hour.count;
    }
    return _arr_minute.count;
    
}
-(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
    UILabel *label_text = [[UILabel alloc]init];
    [label_text setTextAlignment:NSTextAlignmentCenter];
    [label_text setFont:[UIFont systemFontOfSize:15]];
//    label_text.textColor=[UIColor lightGrayColor];
    if (component==0) {
        label_text.text = _arr_year[row];
    }else if (component==1){
        label_text.text =_arr_month[row];
    }else if (component==2){
        label_text.text =_arr_day[row];
    }else if (component==3){
        label_text.text =_arr_hour[row];
    }else{
        
        label_text.text =_arr_minute[row];
    }
    return label_text;
}

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    NSString *title = @"asd";
    if (component==0) {
        title= _arr_year[row];
    }else if (component==1){
        title= _arr_month[row];
        int numvalue = [[_arr_month[row] stringByReplacingOccurrencesOfString:@"月" withString:@""] intValue];
                int yearvalue = [[_arr_year[row] stringByReplacingOccurrencesOfString:@"年" withString:@""] intValue];
        if (numvalue==2 ) {
            if ((yearvalue%4==0&&yearvalue%100!=0)||yearvalue%400==0) {
                //29
                [_arr_day removeObject:@"31日"];
                [_arr_day removeObject:@"30日"];
                if (![_arr_day containsObject:@"28日"]) {
                    [_arr_day addObject:@"28日"];
                }
                if (![_arr_day containsObject:@"29日"]) {
                    [_arr_day addObject:@"29日"];
                }
            }else{
                //28
                [_arr_day removeObject:@"31日"];
                [_arr_day removeObject:@"30日"];
                [_arr_day removeObject:@"29日"];
                if (![_arr_day containsObject:@"28日"]) {
                    [_arr_day addObject:@"28日"];
                }
            }
        }else if(numvalue==4||numvalue==6||numvalue==9||numvalue==11){
            //30
            [_arr_day removeObject:@"31日"];
            if (![_arr_day containsObject:@"28日"]) {
                [_arr_day addObject:@"28日"];
            }
            if (![_arr_day containsObject:@"29日"]) {
                [_arr_day addObject:@"29日"];
            }
            if (![_arr_day containsObject:@"30日"]) {
                [_arr_day addObject:@"30日"];
            }

            
        }else{
            //31

            if (![_arr_day containsObject:@"28日"]) {
                [_arr_day addObject:@"28日"];
            }
            if (![_arr_day containsObject:@"29日"]) {
                [_arr_day addObject:@"29日"];
            }
            if (![_arr_day containsObject:@"30日"]) {
                [_arr_day addObject:@"30日"];
            }
            if (![_arr_day containsObject:@"31日"]) {
                [_arr_day addObject:@"31日"];
            }

        }
    }else if (component==2){
        title= _arr_day[row];
    }else if (component==3){
        title= _arr_hour[row];
    }
    title= _arr_minute[row];
    NSLog(@"%@",title);
    [_pickerView reloadComponent:2];
    
}
-(void)show{
    [self.window_back makeKeyAndVisible];
    _window_back.hidden = NO;
    _isShow = YES;
}
-(void)miss{
    self.window_back.hidden = YES;
    _isShow= NO;
    
}
-(UIWindow *)window_back{
    if (_window_back==nil) {
        _window_back = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
        _window_back.backgroundColor = [UIColor colorWithWhite:0 alpha:0.4];
        [_window_back addSubview:self];
        
    }
    return _window_back;
    
    
}
-(void)buttonCancle{
    [self miss];
}
-(void)buttonSure{
    [self miss];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
